var db = require('../db/commonDB')

module.exports = {
    addMainComment: function (theme_maincom, cb) {
        var sql = `insert into theme_maincom(user_id,theme_id,com_content,com_time,like_count,user_name,user_pic) value(?,?,?,?,?,?,?)`
        var params = [theme_maincom.user_id, theme_maincom.theme_id, theme_maincom.com_content, theme_maincom.com_time, theme_maincom.like_count,theme_maincom.user_name,theme_maincom.user_pic]
        console.log(params);
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('增加主评论失败！'))
                }
            }
        })
    },
    addSubComment: function (theme_subcom, cb) {
        var sql = `insert into theme_subcom(com_id,mainCom_id,user_id,com_content,replied_userId,replied_userName,com_time) value(?,?,?,?,?,?,?)`
        var params = [theme_subcom.com_id, theme_subcom.mainCom_id, theme_subcom.user_id, theme_subcom.com_content, theme_subcom.replied_userId, theme_subcom.replied_userName, theme_subcom.com_time]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('增加子评论失败！'))
                }
            }
        })
    },
    delMainComment: function(theme_maincom,cb){
        var sql = 'delete from theme_maincom where theme_maincom.theme_mainCom_id = ?'
        var params = theme_maincom.theme_mainCom_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('删除主评论失败！'))
                }
            }
        })
    },
    delSubComment: function(theme_subcom,cb){
        var sql = 'delete from theme_subcom where com_id = ?'
        var params = theme_subcom.com_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('删除子评论失败！'))
                }
            }
        })
    },
    getMainComment: function(theme_maincom,cb){
        var sql = 'SELECT * FROM theme_maincom where theme_id = ?'
        var params = theme_maincom.theme_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('查看主评论失败！'))
                }
            }
        })
    },
    getSubComment: function(theme_subcom,cb){
        var sql = 'SELECT com_content FROM theme_subcom where com_id = ?'
        var params = theme_subcom.com_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('查看子评论失败！'))
                }
            }
        })
    },
}